package io.jee.caisan.weixin.config.security;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//	@Bean
//	public SwitchUserFilter switchUserFilter(){
//		SwitchUserFilter switchUserFilter = new SwitchUserFilter();
//		switchUserFilter.setUserDetailsService(userDetailsService);
//		switchUserFilter.setSwitchUserUrl("/admin/**");
//		switchUserFilter.setTargetUrl("/admin/index");
//		switchUserFilter.setExitUserUrl("/admin/logout");
//		return switchUserFilter;
//	}
	

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		//super.configure(http);
		http.authorizeRequests().antMatchers("/admin/**").hasAuthority("admin")
			.and().formLogin().loginPage("/admin/login").defaultSuccessUrl("/admin/index").failureUrl("/admin/login?error").permitAll()
			.and().logout().logoutRequestMatcher(new AntPathRequestMatcher("/admin/logout")).logoutSuccessUrl("/admin/login?logout").permitAll()
			.and().csrf().disable();
	}

	@Override
	public void configure(WebSecurity web) throws Exception {
		web.ignoring().antMatchers("/lib/**", "/img/**", "/api/**", "/**/*.txt", "/assets/**", "/page/**");
	}
	
}